package com.lamchame.webtretho;


import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.lamchame.webtretho.R;


import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.os.Build;

public class MainActivity extends Activity {
	private AdView adView;
	private WebView fullContent;
	private ProgressBar Pbar;
	private WebSettings fullContentsettings;
	//private ActionBar actionBar;
	private static final String AD_UNIT_ID = "ca-app-pub-6575239088642506/1558548874";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
//		actionBar = getActionBar();
//		actionBar.setBackgroundDrawable(getResources().getDrawable(
//				R.drawable.bg));
		
		fullContent = (WebView) findViewById(R.id.webview);		
		fullContent.getSettings().setUseWideViewPort(true);
		Pbar = (ProgressBar) findViewById(R.id.pB1);
		
		fullContent.getSettings().setBuiltInZoomControls(true);
		fullContent.setWebViewClient(new myWebClient());

			fullContent.setWebChromeClient(new WebChromeClient() {
	            public void onProgressChanged(WebView view, int progress) 
	            {
	            if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
	                Pbar.setVisibility(ProgressBar.VISIBLE);
	                //txtview.setVisibility(View.VISIBLE);
	            }
	            Pbar.setProgress(progress);
	            if(progress == 100) {
	                Pbar.setVisibility(ProgressBar.GONE);
	                //txtview.setVisibility(View.GONE);
	            }
	         }
			});
		fullContent.loadUrl("http://webtretho.com");
		fullContentsettings=fullContent.getSettings();
		adView = new AdView(this);
		adView.setAdSize(AdSize.BANNER);
		adView.setAdUnitId(AD_UNIT_ID);
		RelativeLayout rl = new RelativeLayout(this);
		RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.FILL_PARENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		RelativeLayout layout = (RelativeLayout) findViewById(R.id.layoutWeb);
		layout.addView(adView, lay);
		AdRequest adRequest = new AdRequest.Builder().build();
		adView.loadAd(adRequest);
	}
public class myWebClient extends WebViewClient {
		
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// TODO Auto-generated method stub
			//Log.e("onPageStarted", "ok1");
			super.onPageStarted(view, url, favicon);
			//Log.e("onPageStarted", "ok2");
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub
			//Log.e("shouldOverrideUrlLoading", "ok1");
			view.loadUrl(url);
			//Log.e("shouldOverrideUrlLoading", "ok2");
			return true;
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			//Log.e("onPageFinished", "ok1");
			super.onPageFinished(view, url);
//			if (!Config.isTablet){
//				if (Build.VERSION.SDK_INT<maxSdk){
//					fullContentsettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);  
//				}
//				fullContentsettings.setUseWideViewPort(false);
//				//item.setIcon(R.drawable.full_screen);
//				//item.setTitle("Phóng to ảnh");
//				is_Single_Colunm=true;
//			}
		}
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && fullContent.canGoBack()) {
			fullContent.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate menu resource file.
		// Log.e("aaaaaaaaaaaaaaaaa","onCreateOptionsMenu1");
		getMenuInflater().inflate(R.menu.menu_share, menu);
		// Log.e("aaaaaaaaaaaaaaaaa","onCreateOptionsMenu2");
		// Locate MenuItem with ShareActionProvider
		// MenuItem item = menu.findItem(R.id.menu_item_share);
		// Log.e("aaaaaaaaaaaaaaaaa","onCreateOptionsMenu3");
		// Fetch and store ShareActionProvider
		// mShareActionProvider = (ShareActionProvider)
		// item.getActionProvider();
		// Log.e("aaaaaaaaaaaaaaaaa","onCreateOptionsMenu4");
		// mShareActionProvider.setShareIntent(this.getIntent());
		// Return true to display menu
		return true;
		// return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Log.e("aaaaaaaaaaaaaaaaa",String.valueOf(item.getItemId()));
		switch (item.getItemId()) {
		
		case R.id.menu_item_increase_font:
    		//WebSettings fullContentsettings = fullContent.getSettings();
    		fullContentsettings.setTextZoom( (int)(fullContentsettings.getTextZoom() * 1.1) );
    		return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
